// 封装倒计时逻辑函数
import { computed, onUnmounted, ref } from "vue"
import dayjs from "dayjs"

export const useCountDown = () => {
    // 1.响应式数据
    let timer = null
    const formatTime = ref(0)
    const showTime = computed(() => dayjs.unix(formatTime.value).format('mm分ss秒'))
    // 2.开启倒计时的数据
    const start = (currentTime) => {
        formatTime.value = currentTime
        timer = setInterval(() => {
            formatTime.value--
        }, 1000)
    }

    onUnmounted(() => {
        timer && clearInterval(showTime)
    })

    return {
        showTime,
        start
    }
}